package com.ebt.m.policy.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.data.rxModel.apibean.SearchHistory;
import com.ebt.m.policy.bean.EventSearchTagClicked;
import com.ebt.m.widget.flowlayout.FlowLayout;
import com.ebt.m.widget.flowlayout.TagAdapter;
import com.ebt.m.widget.flowlayout.TagFlowLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;


/**
 * @author Damon.Han
 */
public class SearchHistoryContent extends IView {
    private TagFlowLayout mFlowLayout;

    /**
     * 所有标签
     */
    private List<SearchHistory> lables;

    public SearchHistoryContent(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        View view = inflate(context, R.layout.view_online_policy_search_history_content, this);
        mFlowLayout = (TagFlowLayout) view.findViewById(R.id.id_flowlayout);
    }

    public SearchHistoryContent(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SearchHistoryContent(Context context) {
        this(context, null);
    }

    @Override
    public void update(Object... args) {
        List<SearchHistory> lables = (List<SearchHistory>) args[0];
        if (this.lables == null) {
            this.lables = new ArrayList<>();
        }
        this.lables = lables;
        //mFlowLayout.setMaxSelectCount(3);
        mFlowLayout.setAdapter(new TagAdapter<SearchHistory>(lables) {

            @Override
            public View getView(FlowLayout parent, int position, SearchHistory s) {
                TextView tv = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.layout_search_tag, mFlowLayout, false);
                tv.setText(s.getKeyword());
                return tv;
            }
        });

        mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                //view.setVisibility(View.GONE);
                EventSearchTagClicked eventSearchTagClicked = new EventSearchTagClicked();
                eventSearchTagClicked.setSearchHistory(lables.get(position));
                EventBus.getDefault().post(eventSearchTagClicked);
                return true;
            }
        });

    }


}
